﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OGS.Contracts;

namespace OGS.DataAccess
{
    /// <summary>
    /// Represents a class used for database related work of Content
    /// </summary>
    public class DAContent
    {

        private static readonly DAContent instance;

        /// <summary>
        /// Initializes a static instance of the DAContent class with no arguments.
        /// </summary>
        static DAContent()
        {
            instance = new DAContent();
        }

        /// <summary>
        /// Initializes a new instance of the DAContent class with no arguments.
        /// </summary>
        private DAContent() { }

        /// <summary>
        /// The Instance of DAContent class
        /// </summary>
        public static DAContent Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// Saves the Content
        /// </summary>
        /// <param name="objContentInfo"> The ContentInfo Object </param>
        /// <returns>Success/Faliure</returns>
        public int SaveContent(Content objContentInfo)
        {
            using (DBManager dbm = new DBManager("stp_Save_Content"))
			{
				dbm.Add("@ContentID", objContentInfo.ID, ParameterDirection.InputOutput);
				dbm.Add("@Name", objContentInfo.Name);
				dbm.Add("@Description", objContentInfo.Description);
				dbm.Add("@IsActive", objContentInfo.IsActive);
                dbm.Add("@LastModifiedBy", objContentInfo.LastModifiedBy);

				dbm.ExecuteNonQuery();

				objContentInfo.ID = Convert.ToInt32(dbm.GetValueInInt("@ContentID"));
				return Convert.ToInt32(dbm.ReturnValue);
			}
        }

        /// <summary>
        /// Get Content Details
        /// </summary>
        /// <param name="ID"> The Content ID </param>
        /// <returns>The Content </returns>
        public Content GetContentDetails(int ID)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Get_ContentDetails"))
            {
                dbm.Add("@ContentID", ID);

                dt = dbm.GetDataTable();
            }
            Content Content = new Content();
            if (dt.Rows.Count > 0)
            {
                Content = PopulateContent(dt.Rows[0]);
            }

            return Content;
        }

        /// <summary>
        /// Search the Content
        /// </summary>
        /// <param name="objContentInfo"> The ContentInfo Object </param>
        /// <param name="objSearch"> The SearchAttributes Object </param>
        /// <returns>The Content List </returns>
        public List<Content> SearchContent(Content objContentInfo, SearchAttributes objSearch)
        {
            DataTable dt = new DataTable();
            using (DBManager dbm = new DBManager("stp_Search_Content", objSearch))
            {
                dbm.Add("@Name", objContentInfo.Name);
                dt = dbm.GetDataTable();

                objSearch.TotalRecordCount = Convert.ToInt32(dbm.GetValueInInt("@TotalRecordCount"));
            }

            List<Content> Contents = new List<Content>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Contents.Add(PopulateContent(dr));
                }
            }

            return Contents;

        }

        /// <summary>
        /// Populate the Content
        /// </summary>
        /// <param name="dr"> The ContentInfo DataRow </param>
        /// <returns>The Content Object </returns>
        public Content PopulateContent(DataRow dr)
        {
            Content objContentInfo = new Content();

            objContentInfo.ID = Convert.ToInt32(dr["PK_ContentID"]);
            objContentInfo.Name = Convert.ToString(dr["Name"]);
            objContentInfo.Description = Convert.ToString(dr["Description"]);
            objContentInfo.IsActive = Convert.ToBoolean(dr["IsActive"]);
            objContentInfo.LastModifiedDate = Convert.ToDateTime(dr["LastModifiedDate"]);
            objContentInfo.LastModifiedBy = Convert.ToInt32(dr["LastModifiedBy"]);

            return objContentInfo;
        }



    }
}